package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchAttr;
import com.atguigu.gmall.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    @GetMapping("list.html")
    public String list(SearchParam searchParam,Model model){
        Result<Map> result = listFeignClient.list(searchParam);
        //urlParam 表示点击平台属性的路径
        String urlParam = this.makeUrlParam(searchParam);

        String trademarkParam = this.makeTradeMarkParam(searchParam.getTrademark());

        List<SearchAttr> searchAttrList = this.makeSearchAttr(searchParam.getProps());
        Map<String, Object> orderMap = this.makeOrderMap(searchParam.getOrder());
        model.addAllAttributes(result.getData());
        model.addAttribute("searchParam",searchParam);
        model.addAttribute("urlParam",urlParam);
        model.addAttribute("trademarkParam",trademarkParam);
        model.addAttribute("propsParamList",searchAttrList);
        model.addAttribute("orderMap",orderMap);
        return "list/index";
    }

    private Map<String, Object> makeOrderMap(String order) {
        Map<String, Object> map = new HashMap<>();
        //  order=1:asc  order=1:desc   order=2:asc order=2:desc
        if (!StringUtils.isEmpty(order)){
            String[] split = order.split(":");
            if (split!=null&&split.length==2){
                //传递的字段
                map.put("type",split[0]);
                //升序 or 降序
                map.put("sort",split[1]);
            }
        }else{
            map.put("type","1");
            map.put("sort","desc");
        }
        return map;
    }

    //平台属性面包屑集合
    private List<SearchAttr> makeSearchAttr(String[] props) {
        ArrayList<SearchAttr> searchAttrs = new ArrayList<>();
        if (props!=null&&props.length>0){
            for (String prop : props) {
                String[] split = prop.split(":");
                if (split!=null&&split.length==3){
                    SearchAttr searchAttr = new SearchAttr();
                    searchAttr.setAttrId(Long.parseLong(split[0]));
                    searchAttr.setAttrValue(split[1]);
                    searchAttr.setAttrName(split[2]);
                    searchAttrs.add(searchAttr);
                }
            }
        }
        return searchAttrs;
    }
    //处理品牌条件回显
    private String makeTradeMarkParam(String trademark) {
        if (!StringUtils.isEmpty(trademark)) {
            String[] split = trademark.split(":");
            if (split != null && split.length == 2) {
                return "品牌:" + split[1];
            }
        }
        return null;
    }



    //记录用户通过哪些条件进行检索或访问
    private String makeUrlParam(SearchParam searchParam) {
        //字符串拼接 String
        StringBuilder stringBuilder = new StringBuilder();
        //判断用户根据什么条件进行检索
        if (!StringUtils.isEmpty(searchParam.getKeyword())){
            stringBuilder.append("keyword=").append(searchParam.getKeyword());
        }
        //判断用户根据分类Id进行检索
        if (!StringUtils.isEmpty(searchParam.getCategory3Id())){
            stringBuilder.append("category3Id=").append(searchParam.getCategory3Id());
        }

        if (!StringUtils.isEmpty(searchParam.getCategory2Id())){
            stringBuilder.append("category2Id=").append(searchParam.getCategory2Id());
        }

        if (!StringUtils.isEmpty(searchParam.getCategory1Id())){
            stringBuilder.append("category1Id=").append(searchParam.getCategory1Id());
        }
        //判断用户是否根据品牌进行检索
        if (!StringUtils.isEmpty(searchParam.getTrademark())){
            if (stringBuilder.length()>0){
                stringBuilder.append("&trademark=").append(searchParam.getTrademark());
            }
        }
        //根据平台属性值过滤
        String[] props = searchParam.getProps();
        if (props!=null&&props.length>0){
            //循环遍历
            for (String prop : props) {
                if (stringBuilder.length()>0){
                    stringBuilder.append("&props=").append(prop);
                }
            }
        }
        //返回用户的检索数据
        return "list.html?"+stringBuilder.toString();
    }
}
